package generator.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
/**
 * 
 * @author badqiu
 * @email badqiu(a)gmail.com
 */
public class IOHelper {

	public static void copy(Reader in,Writer out) throws IOException {
		int c = -1;
		while((c = in.read()) != -1) {
			out.write(c);
		}
	}
	
	public static String readFile(File file) throws IOException {
		Reader in = new FileReader(file);
		StringWriter out = new StringWriter();
		copy(in,out);
		return out.toString();
	}
	
	public static void saveFile(File file,String content) throws IOException {
		Writer writer = new FileWriter(file);
		writer.write(content);
		writer.close();
	}

	//作者:daniel
	public static void insertContent(File existFile,File insertFile,String beginSign,String endSign) throws IOException {
 
		FileInputStream fis1 = new FileInputStream(existFile.getAbsolutePath());
        int length = fis1.available();
        byte[] bytes = new byte[length];
        fis1.read(bytes);
        fis1.close();
        String existContent = new String(bytes);
        String tempExistContent = existContent;
        ArrayList<String> replacedStr = new ArrayList<String>();//用来存放要被替换的内容
        
        int startIndex = 0;
        int endIndex = 0;
        String replacedString = "";
        
        while(tempExistContent.indexOf(beginSign)!=-1){
        	startIndex = tempExistContent.indexOf(beginSign)+beginSign.length();
        	endIndex = tempExistContent.indexOf(endSign);
        	replacedString = tempExistContent.substring(startIndex, endIndex);
        	replacedStr.add(replacedString);
        	
        	tempExistContent = tempExistContent.replace(replacedString, "");//移除掉内容
        	tempExistContent = tempExistContent.replaceFirst(beginSign, "");//移除掉内容
        	tempExistContent = tempExistContent.replaceFirst(endSign, "");//移除掉内容
        }
        
        
        fis1 = new FileInputStream(insertFile.getAbsolutePath());
        length = fis1.available();
        bytes = new byte[length];
        fis1.read(bytes);
        fis1.close();
        String tempInsertContent = new String(bytes); 
        ArrayList<String> replaceStr = new ArrayList<String>();//用来存放要替换的内容  
        
        while(tempInsertContent.indexOf(beginSign)!=-1){
        	startIndex = tempInsertContent.indexOf(beginSign)+beginSign.length();
        	endIndex = tempInsertContent.indexOf(endSign);
        	replacedString = tempInsertContent.substring(startIndex, endIndex);
        	replaceStr.add(replacedString);
        	
        	tempInsertContent = tempInsertContent.replace(replacedString, "");//移除掉内容
        	tempInsertContent = tempInsertContent.replaceFirst(beginSign, "");//移除掉内容
        	tempInsertContent = tempInsertContent.replaceFirst(endSign, "");//移除掉内容
        }
        
        if(replacedStr.size()>0){
        	for(int i=0;i<replacedStr.size();i++){
        		existContent = existContent.replace(replacedStr.get(i).toString(), replacedStr.get(i).toString()+replaceStr.get(i).toString());
        	}
        }
        
        FileOutputStream fos = new FileOutputStream(existFile.getAbsolutePath());
        byte[] tag_bytes = existContent.getBytes();
        fos.write(tag_bytes);
        fos.close();
        
	}	
	
}
